﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace MVCClubSite.BLL
{
    public class ImageHelper
    {
        const int sizeThumb = 70;

        public static byte[] ConvertImageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms.ToArray();
        }

        public static byte[] MakeThumbnail(byte[] fullsize, int newwidth, int newheight)
        {
            Image iOriginal, iThumb;
            double scaleH, scaleW;

            Rectangle srcRect = new Rectangle();
            iOriginal = Image.FromStream(new MemoryStream(fullsize));
            scaleH = iOriginal.Height / newheight;
            scaleW = iOriginal.Width / newwidth;
            if (scaleH == scaleW)
            {
                srcRect.Width = iOriginal.Width;
                srcRect.Height = iOriginal.Height;
                srcRect.X = 0;
                srcRect.Y = 0;
            }
            else if ((scaleH) > (scaleW))
            {
                srcRect.Width = iOriginal.Width;
                srcRect.Height = Convert.ToInt32(newheight * scaleW);
                srcRect.X = 0;
                srcRect.Y = Convert.ToInt32((iOriginal.Height - srcRect.Height) / 2);
            }
            else
            {
                srcRect.Width = Convert.ToInt32(newwidth * scaleH);
                srcRect.Height = iOriginal.Height;
                srcRect.X = Convert.ToInt32((iOriginal.Width - srcRect.Width) / 2);
                srcRect.Y = 0;
            }
            iThumb = new Bitmap(newwidth, newheight);
            Graphics g = Graphics.FromImage(iThumb);
            g.DrawImage(iOriginal, new Rectangle(0, 0, newwidth, newheight), srcRect, GraphicsUnit.Pixel);
            MemoryStream m = new MemoryStream();
            iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
            return m.GetBuffer();
        }

        public static byte[] ChangeSize(byte[] fullsize, int maxwidth)
        {
            Image iOriginal, iThumb;
            double scale;

            iOriginal = Image.FromStream(new MemoryStream(fullsize));
            if (iOriginal.Width > maxwidth)
            {
                scale = (double)iOriginal.Width / (double)maxwidth;
                int newheight = Convert.ToInt32(iOriginal.Height / scale);
                iThumb = new Bitmap(iOriginal, maxwidth, newheight);

                MemoryStream m = new MemoryStream();
                iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
                return m.GetBuffer();
            }
            else
            {
                return fullsize;
            }
        }
    }
}
